#include "head.h"

int getFileMD5(const char *filename, char *MD5) {
    int fd = open(filename, O_RDONLY);
    MD5_CTX ctx;
    //EVP_MD_CTX *mdctx;
    MD5_Init(&ctx);
    //mdctx = EVP_MD_CTX_new();
    //EVP_DigestInit_ex(mdctx, EVP_md5(), NULL);

    char buf[4096];
    while(1){
        bzero(buf,sizeof(buf));
        ssize_t sret = read(fd,buf,sizeof(buf));
        if(sret == 0){
            break;
        }
        MD5_Update(&ctx,buf,sret);
        //EVP_DigestUpdate(mdctx, buf, sret);
    }
    //unsigned char *md5_digest;
    //unsigned int md5_digest_len = EVP_MD_size(EVP_md5());
    unsigned char md[16];//将要保存md5码16个字节的二进制数据
    MD5_Final(md,&ctx);
    //md5_digest = (unsigned char *)OPENSSL_malloc(md5_digest_len);
    //EVP_DigestFinal_ex(mdctx, md5_digest, &md5_digest_len);
    //EVP_MD_CTX_free(mdctx);
    for (int i = 0; i < 16; i++) {
        sprintf(&MD5[i * 2], "%02x", (unsigned int)md[i]);
    }
    printf("MD5 FROM MD5FUNC: %s\n",MD5);
    return 0;
}
